import tkinter as tk


class LoginUI:
    def __init__(self, root, set_client_id_txt_callback):
        self.root = root
        self.root.title("Login")
        self.set_client_id_txt_callback = set_client_id_txt_callback
        self.client_id_txt = ""

        self.input_frame = tk.Frame(self.root)
        self.input_entry = tk.Entry(self.input_frame, width=40)
        self.input_entry.pack(side=tk.LEFT)
        self.confirm_button = tk.Button(self.input_frame, text="Confirm", command=self.confirm_login)
        self.confirm_button.pack(side=tk.RIGHT)
        self.input_frame.pack()

        # 绑定回车键事件
        self.input_entry.bind("<Return>", lambda event: self.confirm_login())

    def confirm_login(self):
        self.client_id_txt = self.input_entry.get()
        if self.client_id_txt:
            self.set_client_id_txt_callback()
            self.root.destroy()
            print('destroyed')

class EnterRoomUI:
    def __init__(self, root, room_id_callback):
        self.root = root
        self.root.title("加入房间（请输入房间号）")
        self.room_id_callback = room_id_callback
        self.client_id_txt = ""

        self.input_frame = tk.Frame(self.root)
        self.input_entry = tk.Entry(self.input_frame, width=40)
        self.input_entry.pack(side=tk.LEFT)
        self.confirm_button = tk.Button(self.input_frame, text="Confirm", command=self.confirm)
        self.confirm_button.pack(side=tk.RIGHT)
        self.input_frame.pack()

        # 绑定回车键事件
        self.input_entry.bind("<Return>", lambda event: self.confirm())

    def confirm(self): #处理输入的房间号
        self.room_id = self.input_entry.get()
        if self.room_id:
            self.room_id_callback(self.room_id)

class GroupChatUI:
    def __init__(self, root, send_callback, handle_message_callback):
        self.root = root
        self.root.title("Group Chat")
        self.send_callback = send_callback
        self.handle_message_callback = handle_message_callback

        # 创建消息显示框
        self.message_frame = tk.Frame(self.root)
        self.scrollbar = tk.Scrollbar(self.message_frame)
        self.message_list = tk.Listbox(self.message_frame, height=15, width=50, yscrollcommand=self.scrollbar.set)
        self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
        self.message_list.pack(side=tk.LEFT, fill=tk.BOTH)
        self.message_list.pack()
        self.message_frame.pack()

        self.input_frame = tk.Frame(self.root)
        self.input_entry = tk.Entry(self.input_frame, width=40)
        self.input_entry.pack(side=tk.LEFT)
        self.send_button = tk.Button(self.input_frame, text="Send", command=self.send_message)
        self.send_button.pack(side=tk.RIGHT)
        self.input_frame.pack()

        self.input_entry.bind("<Return>", lambda event: self.send_message())

    def send_message(self):
        message = self.input_entry.get()
        if message:
            message = {"type":"message", "content":message}
            print('start send_callback')
            self.send_callback(msg=message)
            self.input_entry.delete(0, tk.END)
            self.root.after(0, lambda: self.message_list.insert(tk.END, f"You: {message['content']}"))

    def display_message(self, msg):
        self.root.after(0, lambda: self.message_list.insert(tk.END, msg))

'''
def open_chat():
    chat_root = tk.Tk()
    # 这里暂时使用空的回调函数，你可以根据实际情况修改
    send_callback = lambda x: None
    handle_message_callback = lambda x: None
    chat_ui = GroupChatUI(chat_root, send_callback, handle_message_callback)
    chat_root.mainloop()'''
